//
//  TripHeaderCell.swift
//  MyImGuider
//
//  Created by 王鹏宇 on 2018/9/20.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

class TripHeaderCell: UITableViewCell {
    
    @IBOutlet weak var containView: UIView!
    
    @IBOutlet weak var containViewHeight: NSLayoutConstraint!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    private var forceReload: Bool = false
    
    var collectionView: UICollectionView!
    var superVC : UIViewController?
    
    let cellWidth = kScreenWidth * 56 / 75
    
    var dataSource: [Calendar]? {
        
        
        willSet {
            
            if let new = newValue, let old = dataSource, new != old {
                
                forceReload = true
            } else {
                
                forceReload = false
            }
        }
        
        didSet {
            
            
            self.collectionView.reloadData()
            
            if forceReload {
                
                self.collectionView.layoutIfNeeded()
                self.collectionView.setContentOffset(CGPoint.zero, animated: false)
            }
            
            if let count = dataSource?.count, count > 1 {
                
                pageControl.isHidden = false
                pageControl.numberOfPages = count
            } else {
                
                pageControl.isHidden = true
            }
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        setup()
    }
    
    private func setup(){
        
        self.selectionStyle = .none
        self.backgroundColor = kThemeBgColor
        self.containView.backgroundColor = kThemeBgColor
        
        let layout = CarouselViewLayout(anim: .carousel1)

        let cellHeight = cellWidth * 733 / 560
        
        layout.scrollDirection = .horizontal
        layout.visbleCount = 8
        layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
        
        self.containViewHeight.constant = CGFloat(cellHeight + 60)
        
        collectionView = UICollectionView(frame:CGRect(x: 0, y: 0, width:kScreenWidth, height: cellHeight + 40), collectionViewLayout: layout)
        collectionView.backgroundColor = kThemeBgColor
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(UINib(nibName: "TripHeadCCell", bundle: nil), forCellWithReuseIdentifier: "TripHeadCCell")
        
        containView.addSubview(collectionView)
    }
}

extension TripHeaderCell: UICollectionViewDelegate,UICollectionViewDataSource {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        var index = Int(ceilf(Float(scrollView.contentOffset.x / CGFloat(cellWidth))))
        
        if index < 0 {
            index = 0
        }
        self.pageControl.currentPage = index
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.dataSource?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TripHeadCCell", for: indexPath) as! TripHeadCCell
        
        cell.calendar = self.dataSource?[indexPath.row]
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let calendar = self.dataSource?[indexPath.row] else {
            
            return
        }
        
        if calendar.planType == .flight {
            
            let airVC = AirplaneDetailVC()
            airVC.viewmodel.calender = calendar
            self.superVC?.navigationController?.pushViewController(airVC, animated: true)
            
            return
        }
        
        let otherVC = OtherDetailVC()
        otherVC.viewmodel.calendar = calendar
        
        self.superVC?.navigationController?.pushViewController(otherVC, animated: true)
    }
}


